//添加校验方法:方法名，校验方法实现，报错信息
jQuery.validator.addMethod("maxByteLength", 
	function(value, element, param) {
		var length = 0;
    	if(length != null){
    		length = value.length + value.replace(/[\u0000-\u00ff]/g, "").length;
    	};
    	return this.optional(element) || (length <= param); 
	}, 
	$.validator.format(getLanMsgs("0ec22180-200195"))
);

//添加校验方法:校验正数
jQuery.validator.addMethod("positiveNumber", 
	function(value, element) {
		return this.optional(element) || /^(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/.test(value);
	}, 
	$.validator.format(getLanMsgs("0ec22180-100169"))
);

//添加校验方法:校验密码格式为6-20位数字和英文字母的组合
jQuery.validator.addMethod("passwordFormat", 
	function(value, element) {
		var reg = /^[A-Za-z].*[0-9]|[0-9].*[A-Za-z]$/;
		return this.optional(element) || ( reg.test(value) && value!=null && value.length>=6 && value.length<=20);
	}, 
	//$.validator.format("密码格式不正确，要求"+param[0] + "-" + param[1] +"位数字和英文字母组合")
	$.validator.format("密码格式不正确，要求6-20位数字和英文字母组合")
);

/**
 * 一些校验框架处理不了的校验
 */

//电子邮箱格式
function checkEmail(emailStr){
	if(emailStr){
		return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(emailStr);
	}else{
		return true;
	}
}

//web网址校验
function checkUrl(urlStr){
	if(urlStr){
		return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(urlStr);
	}else{
		return true;
	}
}

//校验必须为数字
function checkNum(str){
	return /^\d+$/.test(str);
}

//固定电话格式
function checkTel(telStr){
	if(telStr){
		var rexTel = /^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/;
		return rexTel.test(telStr);
	}else{
		return true;
	}
}

//手机格式
function checkMobile(mobileStr){
	if(mobileStr){
		var rexTel = /^((\+86)|(86))?(13)\d{9}$/;
		var rexTel2 = /^(\d{11})$/;
		return rexTel.test(mobileStr) || rexTel2.test(mobileStr);
	}else{
		return true;
	}
}

/**
 * 校验纳税人登记号是否唯一
 * @param taxpayerId
 * @return
 */
function checkTaxPayerIdUnique(){
	$("body").find("[for='taxpayerid_unique']").remove();
	var myts = new Date();
	jQuery.ajax( {
		type : "GET",
		async : false,
		url : ebvp_context + "/register/checkTaxPayerIdUnique?taxpayerid="
				+ escape(encodeURIComponent($("#taxpayerid").val())) + "&myts=" + myts,
		dataType : "text",
		success : function(result) {
			if (result == 'NULL' || result=="") {
				return true;
			}else if(result.indexOf("ERROR:") != -1){
				$("[for='taxpayerid_unique']").html(result);
				return false;
			}else {
				var errMsg = "纳税人登记号已存在，请重新输入!";
				var errLabel = $("<img></img>").attr("for", "taxpayerid_unique")
								.attr("src",
										"/ebvp/commonres/common/frame/src/img/validate_error.png")
								.addClass("error")
								.attr("title", errMsg).attr(
										"style",
										"position:absolute; left:180px; ");
				errLabel.insertAfter($("#taxpayerid"));
				return false;
			}
		},
		error:function(result){
			return true;
		}
	});
}

/**
 * 校验 供应商基本信息变更 的纳税人登记号是否唯一
 * @param taxpayerId
 * @return
 */
function checkSupBasChgTaxPayerIdUnique(){
	$("body").find("[for='taxpayerid_unique']").remove();
	var myts = new Date();
	jQuery.ajax( {
		type : "GET",
		async : false,
		url : ebvp_context + "/register/checkSupBasChagTaxPayerIdUnique?taxpayerid="
				+  escape(encodeURIComponent($("#taxpayerid").val())) + "&myts=" + myts,
		dataType : "text",
		success : function(result) {
			if (result == 'NULL' || result=="") {
				return true;
			}else if(result.indexOf("ERROR:") != -1){
				$("[for='taxpayerid_unique']").html(result);
				return false;
			}else {
				var errMsg = "纳税人登记号已存在，请重新输入!";
				var errLabel = $("<img></img>").attr("for", "taxpayerid_unique")
								.attr("src",
										"/ebvp/commonres/common/frame/src/img/validate_error.png")
								.addClass("error")
								.attr("title", errMsg).attr(
										"style",
										"position:absolute; left:180px; ");
				errLabel.insertAfter($("#taxpayerid"));
				return false;
			}
		},
		error:function(result){
			return true;
		}
	});
}

/**
 * 校验 供应商基本信息变更 的纳税人登记号是否唯一
 * @param taxpayerId
 * @return
 */
function checkSupBasChgTaxPayerIdUnique(){
	$("body").find("[for='taxpayerid_unique']").remove();
	var myts = new Date();
	jQuery.ajax( {
		type : "GET",
		async : false,
		url : ebvp_context + "/register/checkSupBasChagTaxPayerIdUnique?taxpayerid="
				+ escape(encodeURIComponent($("#taxpayerid").val())) + "&myts=" + myts,
		dataType : "text",
		success : function(result) {
			if (result == 'NULL' || result=="") {
				return true;
			}else if(result.indexOf("ERROR:") != -1){
				$("[for='taxpayerid_unique']").html(result);
				return false;
			}else {
				var errMsg = "纳税人登记号已存在，请重新输入!";
				var errLabel = $("<img></img>").attr("for", "taxpayerid_unique")
								.attr("src",
										"/ebvp/commonres/common/frame/src/img/validate_error.png")
								.addClass("error")
								.attr("title", errMsg).attr(
										"style",
										"position:absolute; left:180px; ");
				errLabel.insertAfter($("#taxpayerid"));
				return false;
			}
		},
		error:function(result){
			return true;
		}
	});
}

//校验港华必输的资料类型的附件必须上传
function checkRequiredDoctypeEle(){
	$("body").find("[for='req_doctype_attach']").remove();
	var i=0;
	$(".reqdoctype_tpl").each(function(){
			var val = $($(this).find("[name='finfotype']")[0]).val();
			if(val != null && val != ""){
				if($(this).find(".attach_filename").length == 0){
					var errMsg = "请上传附件!";
					var errLabel = $("<img></img>").attr("for", "req_doctype_attach")
									.attr("src",
											"/ebvp/commonres/common/frame/src/img/validate_error.png")
									.addClass("error")
									.attr("title", errMsg);
					errLabel.insertAfter($($(this).find(".btn_attach")[0]));
				}
			}
	});
}
